/**
 * author: Sheng JIANG
 * date: 2017-04-10
 */

// 全局变量
var flightIndex = {};

$(function () {
    var $document = $(document);
    $document.on("click", ".return-depa", function () {
        var $this = $(this);
        var openClassName = "return-depa-open";
        if (!$this.hasClass(openClassName)) {
            $this.addClass(openClassName);
            $this.off("mouseleave").mouseleave(function () {
                $this.removeClass(openClassName);
            });
        } else {
            $this.removeClass(openClassName);
        }
    });

    // 出发城市选择
    $document.on("click", ".depa-list li", function () {
        var $this = $(this);
        $(".depa-city").html($this.html() + "出发");
        $this.addClass("active").siblings().removeClass("active");
        loadSaleList();
    });

    // 到达城市选择
    $document.on("click", ".return-tab li", function () {
        var $this = $(this);
        $this.addClass("active").siblings().removeClass("active");
        loadSaleList();
    });

    function loadSaleList() {
        $(".return-product").hide();
        var depaId = $(".depa-list").find(".active").data("id");
        var arrivalId = $(".return-tab").find(".active").data("id");
        var $saleList = $("[data-depa=" + depaId + "][data-arrival=" + arrivalId + "]");
        if ($saleList.length) {
            $saleList.show();
        } else {
            flightIndex.getSaleList(depaId, arrivalId);
        }
    }


    var hotSlider = {
        init: function () {
            this.$list = $(".hot-tab-list ul");
            this.$prevBtn = $(".hot-prev-btn");
            this.$nextBtn = $(".hot-next-btn");
            this.$items = this.$list.find("li");
            this.$detailItems = $(".hot-content li");
            this.current = 0;
            this.viewCount = 12;
            this.render();
        },
        render: function () {
            this.length = this.$items.length;
            if (this.length <= this.viewCount) {
                this.disablePrev();
                this.disableNext();
            } else {
                this.bindEvent();
            }
            this.itemWidth = this.$items.eq(0).outerWidth(true);
            this.boxWidth = this.length * this.itemWidth;
            this.$list.css("width", this.boxWidth);
        },
        disablePrev: function () {
            this.$prevBtn.addClass("disabled");
        },
        disableNext: function () {
            this.$nextBtn.addClass("disabled");
        },
        enablePrev: function () {
            this.$prevBtn.removeClass("disabled");
        },
        enableNext: function () {
            this.$nextBtn.removeClass("disabled");
        },
        bindEvent: function () {
            this.$prevBtn.off("click").on("click", {self: this}, this.prev);
            this.$nextBtn.off("click").on("click", {self: this}, this.next);
            this.$items.off("click").on("click", {self: this}, this.select);
        },
        select: function (event) {
            var self = event.data.self;
            var $this = $(this);
            self.$items.removeClass("active");
            $this.addClass("active");
            self.$detailItems.hide().eq($this.index()).show();
        },
        prev: function (event) {
            var self = event.data.self;
            var $this = $(this);
            if ($this.hasClass("disabled")) {
                return false;
            }
            self.setPosition(self.current - 1);
        },
        next: function (event) {
            var self = event.data.self;
            var $this = $(this);
            if ($this.hasClass("disabled")) {
                return false;
            }
            self.setPosition(self.current + 1);
        },
        setPosition: function (index) {
            var diff = this.length - this.viewCount;

            if (index <= 0) {
                index = 0;
                this.disablePrev();
            } else {
                this.enablePrev();
            }
            if (index >= diff) {
                index = diff;
                this.disableNext();
            } else {
                this.enableNext();
            }
            this.current = index;
            var left = -index * this.itemWidth;
            this.$list.stop(false, true).animate({
                "left": left
            }, 300);
        }
    }
    hotSlider.init();

    //region banner交互
    function PicScroll(options) {
        this.init(options);
    }

    PicScroll.prototype = {
        constructor: PicScroll,

        init: function (options) {

            this.delay = options.delay ? options.delay : 5000;
            var self = this;
            this.tabbox = options.tabbox;
            this.picBox = options.picBox;

            this.$index = 0;
            //this.tabs;
            this.pics = this.picBox.find("li");
            this.l = this.pics.length;
            this.Timer = null;
            this.render();

        },
        render: function () {
            var self = this;

            clearTimeout(this.Timer);

            //状态显示及事件
            this.pics.each(function (e) {
                e === 0 ? self.tabbox.append('<li class="active"></li>') : self.tabbox.append("<li></li>");
            });
            this.tabs = this.tabbox.find("li");
            this.autoPlay(); //默认播放
            this.tabs.each(function (e) {
                var me = $(this);
                me.hover(function () {
                    self.$index = e;
                    self.showPic(e);
                    clearTimeout(self.Timer);
                });
            });


            this.picBox.parents('.lv-ban-wrap').hover(function () {
                    clearTimeout(self.Timer);
                }, function () {
                    clearTimeout(self.Timer);
                    self.autoPlay();
                }
            );
        },
        autoPlay: function () {
            var img = new Image();
            var imgs = this.pics.eq(this.$index).find('img');
            var imgSrc = imgs.size() > 0 ? (imgs.attr('to') || imgs.attr('js_to')) : null;

            if (!imgs.attr('src')) {
                if (!imgSrc) {
                    clearTimeout(this.Timer);
                    setTimeout(this.autoPlay, 1000);

                } else {
                    img.onload = function () {
                        this.Play();
                    };
                    img.src = imgSrc;
                }
            } else {
                this.Play();
            }
        },
        Play: function () {
            var self = this;
            self.showPic(self.$index);
            self.Timer = setTimeout(function () {
                self.$index = self.$index >= (self.l - 1) ? 0 : self.$index + 1;
                self.autoPlay();
            }, self.delay);
        },
        showPic: function (i) {


            this.pics.eq(i).addClass("active").siblings().removeClass("active");
            this.tabs.eq(i).addClass("active").siblings().removeClass("active");
        }

    };
    //焦点图
    var tryInitPicScroll_1 = setInterval(function () {

        var size1 = $("#js_allyes_1").children("li").length;
        if (size1 > 0) {
            clearInterval(tryInitPicScroll_1);
            new PicScroll({
                picBox: $(".if-banner-focus .if-bf-images"),
                tabbox: $(".if-banner-focus .if-bf-tabs")
            });
        }


    }, 100);

    $(".JS_tip").poptip({place: 6});
    $(".JS_tip_right").poptip({place: 3});


});
